home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-04-21 | 9.5 KB | 305 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: FWCharIt.cpp
- // Release Version: $ 1.0d1 $
- //
- // Creation Date: 3/28/94
- //
- // Copyright: © 1994 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
-
- #ifndef FWCHARIT_H
- #include "FWCharIt.h"
- #endif
-
- //========================================================================================
- // CLASS FW_CTextReader
- //========================================================================================
-
- #ifdef FW_DEBUG_TEXT_ITERATORS
- //----------------------------------------------------------------------------------------
- // FW_CTextReader::ClassInvariants
- //----------------------------------------------------------------------------------------
-
- void FW_CTextReader::ClassInvariants()
- {
- if (fNext == 0)
- {
- // Must be in initial state, before first GetNextBuffer
- FW_ASSERT(fStart == 0);
- FW_ASSERT(fLimit == 0);
- FW_ASSERT(fBufferSum == 0);
- }
- else if (fNext == fLimit)
- {
- // Must be at end of data structure
- FW_ASSERT(fBufferSum == fLength);
- }
- else
- {
- FW_ASSERT(fBufferSum < fLength);
- if (fNext<fStart)
- {
- // Must be at position -1 (backup past beginning)
- FW_ASSERT(fBufferSum == 0);
- FW_ASSERT(fNext == fStart-1); // may not be correct anymore
- }
- else
- {
- // Must be somewhere in middle of stream
- FW_ASSERT(fStart <= fNext);
- FW_ASSERT(fNext <= fLimit);
- }
- }
- }
- #endif
-
- //----------------------------------------------------------------------------------------
- // FW_CTextReader::FW_CTextReader
- //----------------------------------------------------------------------------------------
-
- FW_CTextReader::FW_CTextReader(const FW_Byte *chunk1Start,
- const FW_Byte *chunk1Limit,
- FW_CharacterCount totalLength) :
- fStart(chunk1Start),
- fLimit(chunk1Limit),
- fNext(chunk1Start),
- fLength(totalLength),
- fBufferSum(0)
- {
- ClassInvariants();
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CTextReader::~FW_CTextReader
- //----------------------------------------------------------------------------------------
-
- FW_CTextReader::~FW_CTextReader()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CTextReader::Advance
- //----------------------------------------------------------------------------------------
-
- void FW_CTextReader::Advance(FW_CharacterCount delta)
- {
- ClassInvariants();
- FW_ASSERT(delta>=0);
- FW_ASSERT(delta+GetPosition() <= fLength);
- FW_CharacterCount inThisBuf = FW_CharactersInBlock(fNext, fLimit-fNext);
- while (delta >= inThisBuf)
- {
- delta -= inThisBuf;
- fNext += FW_BytesInString(fNext, inThisBuf);
- GetNextBuffer();
- inThisBuf = fLimit-fStart;
- }
- fNext += FW_BytesInString(fNext, delta);
- ClassInvariants();
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CTextReader::Backup
- //----------------------------------------------------------------------------------------
-
- void FW_CTextReader::Backup(FW_CharacterCount delta)
- {
- ClassInvariants();
- FW_ASSERT(delta>=0);
- FW_ASSERT(delta<=GetPosition());
- if (fNext==fLimit)
- {
- GetPreviousBuffer();
- fNext = FW_PreviousCharacter(fStart, fLimit);
- delta--;
- }
- FW_CharacterCount inThisBuf = FW_CharactersInBlock(fStart, fNext-fStart);
- while (delta > inThisBuf)
- {
- delta -= inThisBuf;
- fNext = fStart;
- GetPreviousBuffer();
- inThisBuf = FW_CharactersInBlock(fStart, fNext-fStart);
- }
- fNext = FW_PreviousCharacter(fStart, fNext, delta);
- ClassInvariants();
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CTextReader::SetPosition
- //----------------------------------------------------------------------------------------
-
- void FW_CTextReader::SetPosition(FW_CharacterCount position)
- {
- ClassInvariants();
- FW_ASSERT(position>=0);
- FW_ASSERT(position<=fLength);
- FW_CharacterCount curPosition = GetPosition();
- if (position < curPosition)
- Backup(curPosition-position);
- else if (position > curPosition)
- Advance(position-curPosition);
- ClassInvariants();
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CTextReader::GetNextBuffer
- //----------------------------------------------------------------------------------------
-
- void FW_CTextReader::GetNextBuffer()
- {
- // ClassInvariants won't hold here.
- // This function is called to (among other things) restore class invariants.
- FW_ASSERT(fNext == fLimit); // Must hold, but violates invariants
- fBufferSum += FW_CharactersInBlock(fStart, fLimit - fStart);
- if (fBufferSum < fLength)
- {
- DoGetNextBuffer();
- fNext = fStart;
- }
- else
- {
- FW_ASSERT(fBufferSum == fLength);
- fNext = fLimit;
- }
- ClassInvariants();
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CTextReader::GetPreviousBuffer
- //----------------------------------------------------------------------------------------
-
- void FW_CTextReader::GetPreviousBuffer()
- {
- ClassInvariants();
- if (fBufferSum == 0)
- {
- fNext = fStart;
- }
- else if (fBufferSum == fLength)
- {
- FW_ASSERT(fLimit == fNext);
- fBufferSum -= FW_CharactersInBlock(fStart, fLimit - fStart);
- }
- else
- {
- FW_ASSERT(fStart == fNext);
- DoGetPreviousBuffer();
- FW_ASSERT(fStart < fLimit);
- fBufferSum -= FW_CharactersInBlock(fStart, fLimit - fStart);
- FW_ASSERT(fBufferSum >= 0);
- fNext = fLimit;
- }
- // Invariants won't hold here
- // The calling function must modify fNext appropriately
- // Note that GetPreviousBuffer is a protected method,
- // so clients are not held responsible to restore invariants.
- // Also, subclasses shouldn't need to call this function directly.
- }
-
- //========================================================================================
- // CLASS FW_CTextWriter
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // FW_CTextWriter::FW_CTextWriter
- //----------------------------------------------------------------------------------------
-
- FW_CTextWriter::FW_CTextWriter(FW_Byte *chunk1Start,
- FW_Byte *chunk1Limit) :
- fStart(chunk1Start), fNext(chunk1Start), fLimit(chunk1Limit), fBufferSum(0)
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CTextWriter::~FW_CTextWriter
- //----------------------------------------------------------------------------------------
-
- FW_CTextWriter::~FW_CTextWriter()
- {
- }
-
- //========================================================================================
- // CLASS FW_CMemoryReader
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // FW_CMemoryReader::FW_CMemoryReader
- //----------------------------------------------------------------------------------------
-
- FW_CMemoryReader::FW_CMemoryReader(const FW_Byte* buffer,
- FW_ByteCount bytes) :
- FW_CTextReader(buffer, buffer+bytes, FW_CharactersInBlock(buffer,bytes))
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CMemoryReader::FW_CMemoryReader
- //----------------------------------------------------------------------------------------
-
- FW_CMemoryReader::FW_CMemoryReader(const FW_Char* buffer,
- FW_CharacterCount characters) :
- FW_CTextReader((FW_Byte*) buffer, ((FW_Byte*) buffer)+FW_BytesInString(buffer,characters), characters)
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CMemoryReader::~FW_CMemoryReader
- //----------------------------------------------------------------------------------------
-
- FW_CMemoryReader::~FW_CMemoryReader()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CMemoryReader::DoGetNextBuffer
- //----------------------------------------------------------------------------------------
-
- void FW_CMemoryReader::DoGetNextBuffer()
- {
- FW_ASSERT(FALSE);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CMemoryReader::DoGetPreviousBuffer
- //----------------------------------------------------------------------------------------
-
- void FW_CMemoryReader::DoGetPreviousBuffer()
- {
- FW_ASSERT(FALSE);
- }
-
- //========================================================================================
- // CLASS FW_CMemoryWriter
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // FW_CMemoryWriter::FW_CMemoryWriter
- //----------------------------------------------------------------------------------------
-
- FW_CMemoryWriter::FW_CMemoryWriter(FW_Byte* buffer,
- FW_ByteCount capacity) :
- FW_CTextWriter(buffer, buffer+capacity)
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CMemoryWriter::~FW_CMemoryWriter
- //----------------------------------------------------------------------------------------
-
- FW_CMemoryWriter::~FW_CMemoryWriter()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CMemoryWriter::DoFlushAndGetNextBuffer
- //----------------------------------------------------------------------------------------
-
- void FW_CMemoryWriter::DoFlushAndGetNextBuffer()
- {
- FW_ASSERT(FALSE); // Overfilled capacity of buffer!
- }
-
-